package com.sls.web.manager.game.parser;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import com.sls.web.manager.game.pojo.xqt.XQTAccountTypeInfo;
import com.sls.web.manager.game.pojo.xqt.XQTChargetypeInfo;
import com.sls.web.manager.game.pojo.xqt.XQTChoosegem;
import com.sls.web.manager.game.pojo.xqt.XQTDiscountInfo;
import com.sls.web.manager.game.pojo.xqt.XQTGameAreaInfo;
import com.sls.web.manager.game.pojo.xqt.XQTGameServer;
import com.sls.web.manager.game.pojo.xqt.XQTParvalueInfo;

/**
 * 解析xml文件
 * 
 * @author Administrator
 * 
 */
public class ParseXmlDocument {

	private static Logger log = Logger.getLogger(ParseXmlDocument.class);

	/**
	 * 解析星启天gameid.xml
	 * 
	 * @param file
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<XQTDiscountInfo> parseGameIdXml(File file) {
		List<XQTDiscountInfo> records = new ArrayList<XQTDiscountInfo>();
		Document document = null;
		Element root = null;
		try {
			SAXReader saxReader = new SAXReader();
			// 读取XML文件,获得document对象
			document = saxReader.read(file);
			root = document.getRootElement();
			Element secondEle = root.element("records");
			Element thirdEle = null;
			Iterator<Element> it = secondEle.elementIterator();
			while (it.hasNext()) {
				thirdEle = it.next();
				if (thirdEle != null) {
					String tagName = thirdEle.getName();
					if ("record".equals(tagName)) {
						Iterator<Element> it2 = thirdEle.elementIterator();
						XQTDiscountInfo record = new XQTDiscountInfo();
						while (it2.hasNext()) {
							Element fourEle = it2.next();
							if (fourEle != null) {
								String tagName2 = fourEle.getName();
								if ("game".equals(tagName2)) {
									String game = fourEle.getTextTrim();
									record.setRecordName(game);
								} else if ("id".equals(tagName2)) {
									String id = fourEle.getTextTrim();
									record.setRecordId(id);
								} else if ("salePrice".equals(tagName2)) {
									String salePrice = fourEle.getTextTrim();
									record.setRecordSalePrice(salePrice);
								} else if ("proid".equals(tagName2)) {
									String proid = fourEle.getTextTrim();
									record.setRecordProid(proid);
								}
							}
						}
						records.add(record);
					}
				}
			}
		} catch (DocumentException e) {
			log.error(e);
		}

		return records;
	}

	/**
	 * 解析星启天提供的xml模板文件
	 * 
	 * @param file
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, List<Object>> parse(File file) {
		Map<String, List<Object>> map = new HashMap<String, List<Object>>();
		List<Object> list = null;
		Document document = null;
		Element root = null;
		try {
			SAXReader saxReader = new SAXReader();
			document = saxReader.read(file); // 读取XML文件,获得document对象
			String fileName = file.getName();
			String[] fileNames = fileName.split("\\.");
			String subFileName = null;
			if (fileNames.length > 0) {
				subFileName = fileNames[0];
			}
			root = document.getRootElement();
			Iterator<Element> it = root.elementIterator();
			Element secondEle = null;
			String gametypeContent = null;
			while (it.hasNext()) {
				secondEle = it.next();
				if (secondEle != null) {
					String tagName = secondEle.getName();
					if ("order".equals(tagName)) {// 二级节点
						String orderContent = secondEle.getTextTrim();
						list = new ArrayList<Object>();
						list.add(orderContent);
						map.put("order", list);
					} else if ("type".equals(tagName)) {// 二级节点
						String typeContent = secondEle.getTextTrim();
						list = new ArrayList<Object>();
						list.add(typeContent);
						map.put("type", list);
					} else if ("gamecompany".equals(tagName)) {// 二级节点
						String gamecompanyContent = secondEle.getTextTrim();
						list = new ArrayList<Object>();
						list.add(gamecompanyContent);
						map.put("gamecompany", list);
					} else if ("gametype".equals(tagName)) {// 二级节点
						gametypeContent = secondEle.getTextTrim();
						list = new ArrayList<Object>();
						list.add(gametypeContent);
						map.put("gametype", list);
					} else if ("gametype1".equals(tagName)) {// 二级节点
						list = new ArrayList<Object>();
						list.add(subFileName);
						map.put("gametype1", list);
					} else if ("parvalue_s".equals(tagName)) {// 二级节点
						// 解析二级节点parvalue_s
						list = parseSecondNodeParvalues(secondEle, subFileName,
								gametypeContent);
						map.put("parvalue_s", list);
					} else if ("chargetype_s".equals(tagName)) {// 二级节点
						// 解析二级节点chargetype_s
						list = parseSecondNodeChargeTypes(secondEle,
								subFileName);
						map.put("chargetype_s", list);
					} else if ("accounttype_s".equals(tagName)) {// 二级节点
						// 解析二级节点accounttype_s
						list = parseSecondAccountTypes(secondEle, subFileName);
						map.put("accounttype_s", list);
					} else if ("gamearea_s".equals(tagName)) {// 二级节点
						// 解析二级节点gamearea_s
						list = parseSecondGameAreas(secondEle, subFileName);
						map.put("gamearea_s", list);
					}
				}
			}
		} catch (Exception ex) {
			log.error(ex);
		}
		return map;
	}

	/**
	 * 解析二级节点parvalue_s
	 * 
	 * @param secondEle
	 * @param gametype
	 * @param gametype1
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static List<Object> parseSecondNodeParvalues(Element secondEle,
			String gametype1, String gametype) {
		List<Object> list = new ArrayList<Object>();
		Iterator<Element> it = secondEle.elementIterator();
		Element thirdNodeEle = null;
		Element fourNodeEle = null;
		XQTParvalueInfo xqtParvalueInfo = null;
		XQTChargetypeInfo xqtChargetypeInfo = null;
		while (it.hasNext()) {
			xqtParvalueInfo = new XQTParvalueInfo();
			xqtParvalueInfo.setGametype1(gametype1);
			xqtParvalueInfo.setGametype(gametype);
			thirdNodeEle = it.next();
			String name = null;
			String id = null;
			String productid = null;
			String saleprice = null;
			String productid1 = null;
			String sign = null;
			if (thirdNodeEle != null) {
				String tagName = thirdNodeEle.getName();
				if ("parvalue".equals(tagName)) {
					Iterator<Element> iter = thirdNodeEle.elementIterator();
					while (iter.hasNext()) {
						fourNodeEle = iter.next();
						if (fourNodeEle != null) {
							String tagName2 = fourNodeEle.getName();
							if ("name".equals(tagName2)) {
								name = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setParvalueName(name);
							} else if ("id".equals(tagName2)) {
								id = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setParvalueId(id);
							} else if ("productid".equals(tagName2)) {
								productid = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setProductid(productid);
							} else if ("saleprice".equals(tagName2)) {
								saleprice = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setSaleprice(saleprice);
							} else if ("productid1".equals(tagName2)) {
								productid1 = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setProductid1(productid1);
							} else if ("sign".equals(tagName2)) {
								sign = fourNodeEle.getTextTrim();
								xqtParvalueInfo.setSign(sign);
							} else if ("chargetype_s".equals(tagName2)) {
								List<XQTChargetypeInfo> chargetypeList = new ArrayList<XQTChargetypeInfo>();
								Iterator<Element> iterator = fourNodeEle
										.elementIterator();
								while (iterator.hasNext()) {
									Element chargeTypeEle = iterator.next();
									if (chargeTypeEle != null) {
										String tagName4 = chargeTypeEle
												.getName();
										if ("chargetype".equals(tagName4)) {
											Iterator<Element> iterator2 = chargeTypeEle
													.elementIterator();
											xqtChargetypeInfo = new XQTChargetypeInfo();
											xqtParvalueInfo
													.setParvalueName(name);
											while (iterator2.hasNext()) {
												Element fiveNodeEle = iterator2
														.next();
												if (fiveNodeEle != null) {
													String fiveNodeTagName = fiveNodeEle
															.getName();
													if ("name"
															.equals(fiveNodeTagName)) {
														String name2 = fiveNodeEle
																.getTextTrim();
														xqtChargetypeInfo
																.setChargetypeName(name2);
													} else if ("id"
															.equals(fiveNodeTagName)) {
														String id2 = fiveNodeEle
																.getTextTrim();
														xqtChargetypeInfo
																.setChargetypeId(id2);
													} else if ("buynumber_list"
															.equals(fiveNodeTagName)) {
														String buynumber_list = fiveNodeEle
																.getTextTrim();
														xqtChargetypeInfo
																.setBuynumber_list(buynumber_list);
													} else if ("tbuynumber_list"
															.equals(fiveNodeTagName)) {
														String tbuynumber_list = fiveNodeEle
																.getTextTrim();
														xqtChargetypeInfo
																.setTbuynumber_list(tbuynumber_list);
													} else if ("tbuynumbername"
															.equals(fiveNodeTagName)) {
														String tbuynumbername = fiveNodeEle
																.getTextTrim();
														xqtChargetypeInfo
																.setTbuynumbername(tbuynumbername);
													}
												}
											}
											chargetypeList
													.add(xqtChargetypeInfo);
										}
									}
								}
								xqtParvalueInfo.setChargeTypes(chargetypeList);
							} else if ("choosegem_s".equals(tagName2)) {
								List<XQTChoosegem> choosegemList = new ArrayList<XQTChoosegem>();
								Iterator<Element> iterator = fourNodeEle
										.elementIterator();
								while (iterator.hasNext()) {

									Element choosegemEle = iterator.next();
									if (choosegemEle != null) {
										String tagName4 = choosegemEle
												.getName();
										if ("choosegem".equals(tagName4)) {
											Iterator<Element> iterator2 = choosegemEle
													.elementIterator();
											XQTChoosegem choosegem = new XQTChoosegem();
											while (iterator2.hasNext()) {
												Element fiveNodeEle = iterator2
														.next();
												if (fiveNodeEle != null) {
													String fiveNodeTagName = fiveNodeEle
															.getName();
													if ("name"
															.equals(fiveNodeTagName)) {
														String name2 = fiveNodeEle
																.getTextTrim();
														choosegem
																.setName(name2);
													} else if ("id"
															.equals(fiveNodeTagName)) {
														String id2 = fiveNodeEle
																.getTextTrim();
														choosegem.setId(id2);
													}
												}
											}
											choosegemList.add(choosegem);
										}
									}
								}
								xqtParvalueInfo.setChoosegemList(choosegemList);
							}
						}
					}
				}

			}
			list.add(xqtParvalueInfo);
		}
		return list;
	}

	/**
	 * 解析二级节点chargetype_s
	 * 
	 * @param secondEle
	 * @param gametype1
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static List<Object> parseSecondNodeChargeTypes(Element secondEle,
			String gametype1) {
		List<Object> list = new ArrayList<Object>();
		Iterator<Element> it = secondEle.elementIterator();
		Element thirdNodeEle = null;
		Element fourNodeEle = null;
		XQTChargetypeInfo xqtChargetypeInfo = null;
		while (it.hasNext()) {
			thirdNodeEle = it.next();
			if (thirdNodeEle != null) {
				String tagName = thirdNodeEle.getName();
				if ("chargetype".equals(tagName)) {
					Iterator<Element> it2 = thirdNodeEle.elementIterator();
					xqtChargetypeInfo = new XQTChargetypeInfo();
					xqtChargetypeInfo.setGametype1(gametype1);
					while (it2.hasNext()) {
						fourNodeEle = it2.next();
						if (fourNodeEle != null) {
							String tagName2 = fourNodeEle.getName();
							if ("name".equals(tagName2)) {
								String name = fourNodeEle.getTextTrim();
								xqtChargetypeInfo.setChargetypeName(name);
							} else if ("id".equals(tagName2)) {
								String id = fourNodeEle.getTextTrim();
								xqtChargetypeInfo.setChargetypeId(id);
							} else if ("buynumber_list".equals(tagName2)) {
								String buynumber_list = fourNodeEle
										.getTextTrim();
								xqtChargetypeInfo
										.setBuynumber_list(buynumber_list);
							} else if ("tbuynumber_list".equals(tagName2)) {
								String tbuynumber_list = fourNodeEle
										.getTextTrim();
								xqtChargetypeInfo
										.setTbuynumber_list(tbuynumber_list);
							} else if ("tbuynumbername".equals(tagName2)) {
								String tbuynumbername = fourNodeEle
										.getTextTrim();
								xqtChargetypeInfo
										.setTbuynumbername(tbuynumbername);
							}
						}
					}
					list.add(xqtChargetypeInfo);
				}
			}
		}
		return list;
	}

	/**
	 * 解析二级节点accounttype_s
	 * 
	 * @param secondEle
	 * @param gametype1
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static List<Object> parseSecondAccountTypes(Element secondEle,
			String gametype1) {
		List<Object> list = new ArrayList<Object>();
		Iterator<Element> it = secondEle.elementIterator();
		Element thirdNodeEle = null;
		Element fourNodeEle = null;
		XQTAccountTypeInfo xqtAccount = null;
		while (it.hasNext()) {
			thirdNodeEle = it.next();
			if (thirdNodeEle != null) {
				String tagName = thirdNodeEle.getName();
				if ("accounttype".equals(tagName)) {
					Iterator<Element> it2 = thirdNodeEle.elementIterator();
					xqtAccount = new XQTAccountTypeInfo();
					xqtAccount.setGametype1(gametype1);
					while (it2.hasNext()) {
						fourNodeEle = it2.next();
						if (fourNodeEle != null) {
							String tagName2 = fourNodeEle.getName();
							if ("name".equals(tagName2)) {
								String name = fourNodeEle.getTextTrim();
								xqtAccount.setAccounttypeName(name);
							} else if ("id".equals(tagName2)) {
								String id = fourNodeEle.getTextTrim();
								xqtAccount.setAccounttypeId(id);
							}
						}
					}
					list.add(xqtAccount);
				}
			}
		}
		return list;
	}

	/**
	 * 解析二级节点gamearea_s
	 * 
	 * @param secondEle
	 * @param gametype1
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private static List<Object> parseSecondGameAreas(Element secondEle,
			String gametype1) {
		List<Object> list = new ArrayList<Object>();
		Iterator<Element> it = secondEle.elementIterator();
		Element thirdNodeEle = null;
		Element fourNodeEle = null;
		Element fiveNodeEle = null;
		Element sixNodeEle = null;
		XQTGameAreaInfo gameAreaInfo = null;
		while (it.hasNext()) {
			thirdNodeEle = it.next();
			if (thirdNodeEle != null) {
				String tagName = thirdNodeEle.getName();
				if ("gamearea".equals(tagName)) {
					Iterator<Element> it2 = thirdNodeEle.elementIterator();
					gameAreaInfo = new XQTGameAreaInfo();
					gameAreaInfo.setGametype1(gametype1);
					while (it2.hasNext()) {
						fourNodeEle = it2.next();
						String tagName2 = fourNodeEle.getName();
						if ("name".equals(tagName2)) {
							String name = fourNodeEle.getTextTrim();
							gameAreaInfo.setName(name);
						} else if ("id".equals(tagName2)) {
							String id = fourNodeEle.getTextTrim();
							gameAreaInfo.setId(id);
						} else if ("gamesrv_s".equals(tagName2)) {
							Iterator<Element> iter = fourNodeEle
									.elementIterator();
							List<XQTGameServer> gameServers = new ArrayList<XQTGameServer>();
							while (iter.hasNext()) {
								fiveNodeEle = iter.next();
								if (fiveNodeEle != null) {
									String tagName3 = fiveNodeEle.getName();
									if ("gamesrv".equals(tagName3)) {
										Iterator<Element> iterator = fiveNodeEle
												.elementIterator();
										XQTGameServer gameServer = new XQTGameServer();
										gameServer.setGametype1(gametype1);
										while (iterator.hasNext()) {
											sixNodeEle = iterator.next();
											if (sixNodeEle != null) {
												String tagName4 = sixNodeEle
														.getName();
												if ("name".equals(tagName4)) {
													String name = sixNodeEle
															.getTextTrim();
													gameServer.setName(name);
												} else if ("id"
														.equals(tagName4)) {
													String id = sixNodeEle
															.getTextTrim();
													gameServer.setId(id);
												}
											}
										}
										gameServers.add(gameServer);
									}
								}
							}
							gameAreaInfo.setGameSerList(gameServers);
						}
					}
				}
			}
			list.add(gameAreaInfo);
		}
		return list;
	}
}
